package com.ereport.controller.adminController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.ExecutiveDAO;
import com.ereport.dao.NewsUpdateDao;
import com.ereport.entity.Executives;
import com.ereport.entity.NewsUpdate;
import com.ereport.entity.PublishNews;
@Controller
@RequestMapping("/newsUpdate")
public class NewsUpdateController {
	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		model.setViewName("/jsp/admin/createNews.jsp");
		model.addObject("menuItem","create");
		return model;
	}
	@RequestMapping("/publishList.do")
	public ModelAndView publishList(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<NewsUpdate> nulist=new ArrayList<NewsUpdate>();
		try {
			nulist=nudao.getAllHeadlines();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/admin/publishNewsList.jsp");
		model.addObject("menuItem","publish");
		model.addObject("nulist", nulist);
		return model;
	}
	@RequestMapping("/newsListOfExecutive.do")
	public ModelAndView newsListOfExecutive(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<NewsUpdate> nulist=new ArrayList<NewsUpdate>();
		try {
			nulist=nudao.getAllHeadlinesOfExecutive(ex.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/publishNewsListOfExecutive.jsp");
		model.addObject("nulist", nulist);
		return model;
	}
	@RequestMapping("/getNewsDetails.do")
	public @ResponseBody String getNewsDetails(HttpServletRequest req,HttpSession session){
		Executives ex=(Executives)session.getAttribute("loginUser");
		int nuid=Integer.parseInt(req.getParameter("newsid"));
		NewsUpdateDao nudao=new NewsUpdateDao();
		NewsUpdate nu=new NewsUpdate();
		try {
			nu=nudao.findById(nuid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		StringBuilder sb=new StringBuilder();
		sb.append(nu.getHeadLine()+"|");
		sb.append(nu.getLiterature()+"|");
		sb.append(nu.getActionToBeTaken());
		return sb.toString();
	}
	@RequestMapping("/deleteNewsList.do")
	public ModelAndView deleteList(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<NewsUpdate> nulist=new ArrayList<NewsUpdate>();
		try {
			nulist=nudao.getAllHeadlines();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/admin/deleteNewsFromList.jsp");
		model.addObject("menuItem","delete");
		model.addObject("nulist", nulist);
		return model;
	}
	@RequestMapping("searchHeadLines.do")
	public @ResponseBody
	String searchHeadLines(HttpServletRequest req,HttpSession session){
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<NewsUpdate> nulist=new ArrayList<NewsUpdate>();
		try {
			nulist=nudao.getAllHeadlines();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator itr=nulist.iterator();
		StringBuilder sb=new StringBuilder();
		while(itr.hasNext()){
			Object obj=itr.next();
			NewsUpdate  nu=(NewsUpdate)obj;
			sb.append(nu.getId()+"/"+nu.getHeadLine()+",");
		}
		return sb.toString();
		
	}
	@RequestMapping("/getHeadLines.do")
	public ModelAndView getHeadLines(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String news=req.getParameter("headLinesName");
		if(!news.equals("")){
			int newid=Integer.parseInt(news);
			NewsUpdateDao nudao=new NewsUpdateDao();
			NewsUpdate nu=new NewsUpdate();
			try {
				nu=nudao.findById(newid);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			model.setViewName("/jsp/admin/editNews.jsp");
			model.addObject("news",nu);
			model.addObject("menuItem","create");
			return model;
		}else {
			model.setViewName("/jsp/admin/createNews.jsp");
			model.addObject("menuItem","create");
			return model;
		}
		
		
	}
	@RequestMapping("/create.do")
	public ModelAndView save(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String news=req.getParameter("newsname");
		
		String headLine=req.getParameter("headLine");
		String literature=req.getParameter("literature");
		String actToBeTaken=req.getParameter("actToBeTaken");
		Date cdate=new Date();
		cdate=new java.sql.Date(cdate.getTime());
		NewsUpdateDao nudao=new NewsUpdateDao();
		NewsUpdate nu=null;
		if(news!=null){
			int newid=Integer.parseInt(news);
			nu=new NewsUpdate(newid,headLine,literature,actToBeTaken,cdate,ex.getId());
			List<PublishNews> pnlist=new ArrayList<PublishNews>();
			try {
				pnlist=nudao.getPublishNewsByNewsId(newid);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			model.addObject("pnlist", pnlist);
		}else{
			nu=new NewsUpdate(headLine,literature,actToBeTaken,cdate,ex.getId());
		}
		if(!headLine.equals("")){
		int c=0;
		try {
			c=nudao.testHeadline(headLine);
			System.out.println("count headlines "+c); 
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		if(c==0){
		System.out.println("news update "+nu);
		try {
			nudao.insert(nu);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			nu=nudao.getMaxId();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ExecutiveDAO edao=new ExecutiveDAO();
		List<Executives> exlist=null;
		try {
			exlist=edao.getExecutives();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.setViewName("/jsp/admin/publishNews.jsp");
		model.addObject("news", nu);
		model.addObject("exlist", exlist);
		model.addObject("msg", "News Created Successfully");
		model.addObject("menuItem", "publish");
		return model;
		}else {
			model.setViewName("/jsp/admin/createNews.jsp");
		model.addObject("msg", "News Head Line Already Exist");
		model.addObject("menuItem","create");
		return model;
		}
		
		}else {
			model.setViewName("/jsp/admin/createNews.jsp");
			model.addObject("msg", "News Head Line Should Not Be Empty");
			model.addObject("menuItem","create");
			return model;
		}
		
	}
	@RequestMapping("/delete.do")
	public ModelAndView delete(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String newsListSize=req.getParameter("newsListSize");
		int nls=0;
		if(newsListSize!=null){
			nls=Integer.parseInt(newsListSize);
		}
		List<Integer> nulist=new ArrayList<Integer>();
		for(int i=0;i<nls;i++){
			String newsId=req.getParameter("newsCheckBox"+(i+1));
			if(newsId!=null){
				int nid=Integer.parseInt(newsId);
				nulist.add(nid);
				NewsUpdateDao nudao=new NewsUpdateDao();
				try {
					nudao.deleteNews(nulist);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			model.setViewName("redirect:deleteNewsList.do");
			model.addObject("msg", "News Deleted Successfully");
			model.addObject("menuItem","delete");
			return model;
		}
	@RequestMapping("/publishNews.do")
	public ModelAndView publishNews(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		int newid=Integer.parseInt(req.getParameter("newsid"));
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<PublishNews> pnlist=new ArrayList<PublishNews>();
		try {
			pnlist=nudao.getPublishNewsByNewsId(newid);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		NewsUpdate nu=new NewsUpdate();
		try {
			nu=nudao.findById(newid);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ExecutiveDAO edao=new ExecutiveDAO();
		List<Executives> exlist=null;
		try {
			exlist=edao.getExecutives();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.setViewName("/jsp/admin/publishNews.jsp");
		model.addObject("news", nu);
		model.addObject("exlist", exlist);
		model.addObject("pnlist", pnlist);
		model.addObject("menuItem", "publish");
		return model;
	}
	@RequestMapping("/publish.do")
	public ModelAndView publish(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String newsid=req.getParameter("newsid");
		String exelistSize=req.getParameter("exelistSize");
		int nwid = Integer.parseInt(newsid);
		int exlsize = Integer.parseInt(exelistSize);
		Date cdate=new Date();
		cdate=new java.sql.Date(cdate.getTime());
		NewsUpdateDao nudao=new NewsUpdateDao();
		List<PublishNews> pnlist=new ArrayList<PublishNews>();
		for(int i=0;i<exlsize;i++){
			String exid=req.getParameter("exe"+(i+1));
			if(exid!=null){
				int eid=Integer.parseInt(exid);
				PublishNews pn=new PublishNews(nwid,eid,cdate,ex.getId());
				pnlist.add(pn);
			}
		}
		if(pnlist.size()>0){
		try {
			nudao.insertPublish(pnlist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("redirect:open.do");
		model.addObject("msg", "News Published Successfully");
		return model; 
		}else{
			model.setViewName("redirect:open.do");
			model.addObject("msg", "News Not Published to Any Executive");
			return model;
		}
		
	}
	
}
